var $ = require("../../utils/Ajax.js");
Page({
  data: {
    Optional: { Status: true, Img:"https://sl.sx-sss.com/xcx/images/OptionalStatus.png"},
    Enterprise: false,
    Type: 1,

    // 发票抬头
    Title: null,

    // 税号
    Duty: null,

    // 开户银行
    Name: null,

    // 银行账号
    Account: null,

    // 企业地址
    Address: null,

    // 企业电话
    Phone: null,

    // 是否为默认
    IsDefault: 1,

    // 发票内容
    Content: ["商品明细", "商品类别"],
    index: 0,
  },

  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: "发具发票"
    })
  },

  onShow: function () {
    var Invoice = wx.getStorageSync('Invoice');
    console.log(Invoice)
    if (Invoice.length != 0){
      this.setData({
        Type: Invoice.type,
        Title: Invoice.title,
        Duty: Invoice.identificationNumber,
        Name: Invoice.bank,
        Account: Invoice.cardNum,
        Address: Invoice.address,
        Phone: Invoice.tel,
        IsDefault: Invoice.isDefault
      })
    }
  },

  // 发票类型
  InvoiceType: function(e){
    var Type = e.detail.value;
    if (Type == 0){
      this.setData({
        Type: Type,
        Enterprise: true,
        Duty: null,
        Name: null,
        Account: null,
        Address: null,
        Phone: null,
        Optional: { Status: true, Img: "https://sl.sx-sss.com/xcx/images/OptionalStatus.png" }
      })
    }else{
      this.setData({
        Type: Type,
        Enterprise: false
      })
    }
  },

  // 发票抬头
  Title: function(e){
    var Title = e.detail.value;
    this.setData({
      Title: Title
    })
  },

  // 税号
  Duty: function(e){
    var Duty = e.detail.value;
    this.setData({
      Duty: Duty
    })
  },

  // 开户银行
  Name: function(e){
    var Name = e.detail.value;
    this.setData({
      Name: Name
    })
  },

  // 银行账号
  Account: function(e){
    var Account = e.detail.value;
    this.setData({
      Account: Account
    })
  },

  // 企业地址
  Address: function(e){
    var Address = e.detail.value;
    this.setData({
      Address: Address
    })
  },

  // 企业电话
  Phone: function(e){
    var Phone = e.detail.value;
    if (Phone.length == 11){
      this.setData({
        Phone: Phone
      })
    }else{
      wx.showToast({
        title: '手机号码错误!',
        icon: 'none',
        duration: 1000,
        mask: true
      })
    }
  },

  // 是否为默认地址
  IsDefaultSelect: function(e){
    var IsDefault = e.detail.value;
    if (IsDefault.length == 1){
      this.setData({
        IsDefault: 1
      })
    }else{
      this.setData({
        IsDefault: 0
      })
    }
  },

  // 点击展示选填信息
  OptionalStatus: function(){
    var Status = this.data.Optional.Status;
    if (Status){
      this.setData({
        Optional: { Status: false, Img: "https://sl.sx-sss.com/xcx/images/OptionalStatus_.png" }
      })
    }else{
      this.setData({
        Optional: { Status: true, Img: "https://sl.sx-sss.com/xcx/images/OptionalStatus.png" }
      })
    }
  },

  // 选择发票内容
  InvoiceContent: function (e) {
    this.setData({
      index: e.detail.value
    })
  },

  // 返回
  Back: function(){
    wx.navigateBack({
      delta: 1
    })
  },

  // 点击完成按钮
  InvoiceOk: function(){
    var that = this;
    var Type = that.data.Type;
    var Title = that.data.Title;
    var Duty = that.data.Duty;
    var Name = that.data.Name;
    var Account = that.data.Account;
    var Address = that.data.Address;
    var Phone = that.data.Phone;
    var Content = that.data.Content[that.data.index];
    var IsDefault = that.data.IsDefault
    if (Type == '' || Type == null || Title == '' || Title == null){
      wx.showToast({
        title: '必填信息不能为空',
        icon: 'none',
        duration: 1000,
        mask: true
      })
    }else{
      $.Ajax("api/addInvoice","POST",{
        type: Type,
        title: Title,
        identificationNumber: Duty,
        cardNum: Account,
        bank: Name,
        address: Address,
        tel: Phone,
        content: Content,
        isDefault: IsDefault
      },function(res){
        console.log(res)
        if(res.s){
          wx.setStorageSync('Invoice', res.d.invoice);
        }
        wx.showToast({
          title: res.m,
          icon: 'none',
          duration: 1500,
          mask: true
        })
        setTimeout(function(){
          wx.navigateBack({
            detail: 1
          })
        },2000)
      })
    }
  }
})